-- Copyright (c) 2025 Huawei Technologies Co., Ltd.
-- openUBMC is licensed under Mulan PSL v2.
-- You can use this software according to the terms and conditions of the Mulan PSL v2.
-- You may obtain a copy of Mulan PSL v2 at: http://license.coscl.org.cn/MulanPSL2
-- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
-- EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
-- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
-- See the Mulan PSL v2 for more details.

local class = require 'mc.class'
local singleton = require 'mc.singleton'
local log = require 'mc.logging'
local base_msg = require 'messages.base'
local trusted_component = require 'domain.trusted_component'
local component_integrity = require 'domain.component_integrity'

local responder = class()

function responder:ctor(chassis_id, device)
    self.m_chassis_id = chassis_id
    self.m_device_name = device.device_name
    self.m_mdb_name = device.mdb_name
    self.m_component = nil
    self.m_integrity_collection = {}
end

function responder:init()
end

function responder:new_component(chassis_id, device, inventory_path, type)
    if self.m_component then
        log:error("trusted component existed, can not create")
        error(base_msg.InternalError())
    end
    self.m_component = trusted_component.new(chassis_id, device, inventory_path, type)
end

function responder:new_integrity(integrity_info, measurement_info)
    local id = #self.m_integrity_collection + 1
    self.m_integrity_collection[id] =
        component_integrity.new(id, self.m_chassis_id, self.m_mdb_name, integrity_info, measurement_info)

    return id
end

function responder:get_measurement(id, index)
    if not self.m_integrity_collection[id] then
        log:error("trusted component existed, can not create")
        return nil
    end

    return self.m_integrity_collection[id].m_measurement_set[index]
end

return singleton(responder)